# name : gsDesign gsSurv full
# key : gsDesign.gsSurv.full
# key : gsDesign.gsSurv.template
# expand-env: ((yas-indent-line 'fixed) (yas-wrap-around-region 'nil))
# contributor: Shuguang Sun
# --
# median control time-to-event
medianC <- ${1:12}
# exponential dropout rate per unit of time
eta <- ${2:0.001}
# hypothesized experimental/control hazard ratio
# (alternate hypothesis)
hr <- ${3:0.6}
# null hazard ratio (1 for superiority, >1 for non-inferiority)
hr0 <- ${4:1}
# Type I error (1-sided)
alpha <- ${5:0.025}
# Type II error (1-power)
beta <- ${6:0.1}
# study duration, NULL is to calcuate, otherwise gamma is relative
T <- ${7:NULL}
# follow-up duration of last patient enrolled
minfup <- ${8:5}
# enrollment period durations
R <- rep(1, 10)
# relative enrollment rates during above periods, same length as R
gamma <- c(0.25, 0.25, 0.25, 0.50, 0.50, 0.50, 0.75, 0.75, 0.75, 1.00) * ${9:20}
# randomization ratio, experimental/control
ratio <- ${10:1}
# number of analyses (interim + final)
k <- ${11:2}
# timing of interim analyses (k-1 increasing numbers >0 and <1)
# proportion of final events at each interim
timing <- ${12:0.67}
# efficacy bound spending function
# We use Lan-DeMets spending function approximating O'Brien-Fleming bound
# no parameter required for this spending function
sfu <- ${13:$$(yas-choose-value '("sfLDOF" "sfLDPocock" "sfHSD" "OF" "Pocock" "WT"))}
sfupar <- NULL
# futility bound spending function
sfl <- ${14:$$(yas-choose-value '("sfHSD" "sfLDOF" "sfLDPocock"))}
# futility bound spending parameter specification
sflpar <- ${15:-7}
# generate design
${16:gs1} <- gsSurv(k = k, timing = timing, R = R, gamma = gamma, eta = eta,
            minfup = minfup, T = T, lambdaC = log(2) / medianC,
            hr = hr, hr0 = hr0 , beta = beta, alpha = alpha, ratio = ratio,
            sfu = sfu, sfupar = sfupar,
            # sfl = sfl, sflpar = sflpar,
            test.type = 1)
$16


library(gt)
library(tibble)
tibble(Period = paste("Month",rownames($16$gamma)),
       Rate = as.numeric($16$gamma)) %>%
  gt() %>% tab_header(title = "Enrollment rate requirements")

# footnote te$16t for table
footnote1 <- "P{Cross} is the probability of crossing the given bound (efficacy or futility) at or before the given analysis under the assumed hazard ratio (HR)."
footnote2 <- " Design assumes futility bound is discretionary (non-binding); upper boundary crossing probabilities shown here assume trial stops at first boundary crossed and thus total less than the design Type I error."
footnoteHR <- "HR presented is not a requirement, but an estimate of approximately what HR would be required to cross each bound."
footnoteM <- "Month is approximated given enrollment and event rate assumptions under alternate hypothesis."
# spending function footnotes
footnoteUS <-"Efficacy bound set using Lan-DeMets spending function approximating an O'Brien-Fleming bound."
footnoteLS <- paste("Futility bound set using ", $16$lower$name,
                    " beta-spending function with ", $16$lower$parname,
                    "=", $16$lower$param, ".", sep = "")
# caption text for table
caption <- paste("Overall survival trial design with HR=", hr, ", ",
                 100*(1-beta), "% power and ", 100*alpha,"% Type 1 error",sep = "")

as_tibble(gsBoundSummary($16)) %>%
  gt() %>%
  tab_header(title = "Time-to-event group sequential design") %>%
  cols_align("left") %>%
  tab_footnote(footnoteUS, locations = cells_column_labels(columns = 3)) %>%
  ## tab_footnote(footnoteLS, locations = cells_column_labels(columns = 4)) %>%
  tab_footnote(footnoteHR, locations = cells_body(columns = 2,rows = c(3,8))) %>%
  tab_footnote(footnoteM, locations = cells_body(columns = 1,rows = c(4,9))) %>%
  tab_footnote(footnote1, locations = cells_body(columns = 2,rows = c(4,5,9,10))) %>%
  tab_footnote(footnote2, locations = cells_body(columns = 2,rows = c(4,9)))
